package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.GlobalHttpHandlerImpl;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.api.Api;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqLoginBean;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqThirdLogin;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseJson;
import com.ewzj.bigdata.mvp.model.entity.resbean.TokenBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.UserBean;
import com.google.gson.Gson;
import com.jess.arms.base.App;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.LoginFragmentContract;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@ActivityScope
public class LoginFragmentModel extends BaseModel implements LoginFragmentContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public LoginFragmentModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        service=repositoryManager.obtainRetrofitService(CommonService.class);
        this.mApplication = application;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<UserBean> login(String phone, String pass) {
        ReqLoginBean reqLoginBean=new ReqLoginBean(phone,pass);
        return service.userLogin(Apputils.json2Body(mGson.toJson(reqLoginBean)))
                .map(new Function<BaseData<UserBean>, UserBean>() {
                    @Override
                    public UserBean apply(BaseData<UserBean> userBeanBaseData) throws Exception {
                        return userBeanBaseData.getData();
                    }
                });
    }

    @Override
    public Observable<TokenBean> loginByThird(String token, String openid, String name) {
        ReqThirdLogin reqThirdLogin=new ReqThirdLogin(token,openid,name.toLowerCase(),"android");
        return service.getDataFromService(Apputils.getFromatUrl(Api.THIRD_LOGIN),Apputils.json2Body(mGson.toJson(reqThirdLogin)))
                .map(new Function<BaseJson, TokenBean>() {
                    @Override
                    public TokenBean apply(BaseJson baseJson) throws Exception {
//                        TokenBean tokenBean=mGson.fromJson(GlobalHttpHandlerImpl.decodeFromJNI(baseJson.getCipher()),TokenBean.class);
                        TokenBean tokenBean=new TokenBean();
                        return tokenBean;
                    }
                });
    }
}